/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package remote;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


/**
 *
 * @author Nuno
 */
public class nfs {
    private String URL;
    private URLConnection c;

    public nfs(String URL){
        this.URL = URL;

    }

    public boolean connect() throws MalformedURLException, IOException{
        URL url2 = new URL(URL);
        c = url2.openConnection();
        c.setDoInput(true);
        
        if (!c.getDoInput())
            return false;
        return true;
    }

    

    public synchronized Folder getContent() throws IOException{
        int i=0;
        while(c.getHeaderField(i)!=null){
            System.out.println(c.getHeaderField(i)); // se for uma pasta apenas tem o indice 0
            i++;
            }

        Folder fold = new Folder("/");

        InputStream inStream = c.getInputStream();
            BufferedReader input =
            new BufferedReader(new InputStreamReader(inStream));
            String line = "";
            while ((line = input.readLine()) != null){
                getContent(line, fold);
            }

        return fold;
    }

    public synchronized void getContent(String path, Folder fold) throws MalformedURLException, IOException{
        URL url_t = new URL(URL+"/"+path);
        URLConnection ct = url_t.openConnection();
        ct.setDoInput(true);
            
        if(ct.getHeaderField(1)==null){

            Folder ff = new Folder(path.substring(path.lastIndexOf("/")+1, path.length()));
            fold.addFolder(ff);


            InputStream inStream = ct.getInputStream();
            BufferedReader input = new BufferedReader(new InputStreamReader(inStream));
            String line = "";
            while ((line = input.readLine()) != null){
                getContent(path+"/"+line, ff);
            }
        }else{

            fold.addFile(new rFile(path.substring(path.lastIndexOf("/")+1, path.length())));
        }
    }

     public synchronized String downloadFile(String filename) throws IOException{
        URL url_t = new URL(URL+"/"+filename);
        URLConnection ct = url_t.openConnection();
        ct.setDoInput(true);
         
        InputStream inStream = ct.getInputStream();   
        BufferedInputStream input = new BufferedInputStream(inStream);

        filename = filename.substring(filename.lastIndexOf("/")+1, filename.length());
        File f = new File("/" + filename);
        int i=1, ind;

        ind = filename.lastIndexOf(".");

        String eFilename = filename;
        while(f.exists()){

            eFilename = filename.substring(0, ind) + "(" + i + ")." + filename.substring(ind+1, filename.length());
            f=new File(eFilename);
            i++;
        }



        OutputStream os = new FileOutputStream("temp/" + eFilename);
        BufferedOutputStream bos = new BufferedOutputStream(os);

        progress p = new progress(ct.getHeaderField(1), f);
        p.start();

        byte[] buffer = new byte[1024];
        int readCount;

        while( (readCount = input.read(buffer)) > 0)
        {
            bos.write(buffer, 0, readCount);
        }
        bos.close();
        input.close ();
        return eFilename;

     }

}
